<?php
/**
 * created by zzy
 * date: 2017/10/24 9:28
 */


namespace App\Http\Admin\Company\Controllers;
use Framework\BaseClass\Http\Admin\Controller;
use App\Eloquent\Ygt\Permission;
use App\Eloquent\Ygt\PrivilegePermission;

class PermissionController extends Controller
{
    public function index()
    {
    }
    public function lists()
    {
        $admin_user         = request()->user('admin')->toArray();
        $company_id         = $admin_user['company_id'];

        if (request()->isMethod('post')) {

            $where          = ['company_id'=>$company_id];
            $lists          = Permission::getList( $where );
            return $lists->toJson();
        }
        return $this->view('lists' );
    }
    public function edit()
    {
        $admin_user         = request()->user('admin')->toArray();
        $company_id         = $admin_user['company_id'];

        $params             = $this->requestJson();
        $id                 = isset($params['id']) ? $params['id'] : 0;
        $title              = $params['title'];

        $data               = [ 'title'=>$title ];
        if( $id ){
            $where          = ['company_id'=>$company_id,'id'=>$id];
            $result         = Permission::updateOneData( $where, $data );
        } else {
            $data['company_id']     = $company_id;
            $result                   = Permission::insertOneData( $data, 'id' );
        }
        if( $result ){
            return $this->ajaxSuccess( '成功' );
        } else {
            return $this->ajaxFail( '失败' );
        }
    }
    public function delete()
    {
        $admin_user         = request()->user('admin')->toArray();
        $company_id         = $admin_user['company_id'];

        $id                 = request( 'id', 0 );
        if( $id ){
            $id_arr         = explode(',', $id);
            $where          = ['company_id'=>$company_id,'id'=>['in',$id_arr]];
            $del_result     = Permission::del($where);
            if( $del_result !== false ){
                $where      = ['permission_id'=>['in',$id_arr]];
                $del_result = PrivilegePermission::del( $where );
                if( $del_result !== false ){
                    return $this->ajaxSuccess('成功');
                } else {
                    return $this->ajaxFail('失败3');
                }
            } else {
                return $this->ajaxFail('失败1');
            }
        } else {
            return $this->ajaxFail('失败2');
        }
    }
}